package com.laicunba.ext.web.controller.console.sys;

import java.util.HashMap;
import java.util.Map;

import org.json.simple.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.laicunba.ext.web.controller.BaseController;
import com.laicunba.pojo.ScriptDO;
import com.laicunba.service.ScriptService;
import com.laicunba.util.DateUtil;
import com.laicunba.util.StringUtil;

@Controller
@SuppressWarnings("unchecked")
@RequestMapping(value = "/console/ext/sys")
public class ScriptController extends BaseController {
	@Autowired
	private ScriptService scriptService;

	@RequestMapping(value = "/script_list.htm")
	public ModelAndView scriptList() {
		ModelAndView view = new ModelAndView("script_list.jsp");
		view.addObject("scriptList", scriptService.list());
		return view;
	}

	@ResponseBody
	@RequestMapping(value = "/script_listen_status_list.htm")
	public String scriptlistenStatusList() {
		Map map = new HashMap();
		map.put("jms-listen", scriptService.findJmsListenScriptIds());
		map.put("act-listen", scriptService.findActListenScriptIds());
		return toJsonObject(map);
	}

	@ResponseBody
	@RequestMapping(value = "/script_execute_log.htm")
	public String scriptExecuteLog(String scriptId) {
		return toJsonArray(scriptService.log().get(scriptId));
	}

	@RequestMapping(value = "/script_form.htm")
	public ModelAndView scriptForm(String id) {
		ModelAndView view = new ModelAndView("script_form.jsp");
		view.addObject("scriptDO", scriptService.get(ScriptDO.class, id));
		return view;
	}

	@ResponseBody
	@RequestMapping(value = "/script_del.htm")
	public String scriptDel(String id) {
		JSONObject json = new JSONObject();
		json.put("code", scriptService.delete(ScriptDO.class, id));
		json.put("id", id);
		return json.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/script_update_status.htm")
	public String scriptUpdateStatus(String id,String newStatus) {
		JSONObject json = new JSONObject();
		ScriptDO  scriptDO = scriptService.get(ScriptDO.class, id);
		if(scriptDO==null){ 
			json.put("code", "SCRIPT_DO_NOT_EXIST");
			return json.toJSONString();
		} 
		scriptDO.setStatus(newStatus);
		json.put("code", scriptService.updateSkipCheckOwner(scriptDO));
		json.put("id", id);
		json.put("newStatus", newStatus);
		return json.toJSONString();
	}	
	
	@ResponseBody
	@RequestMapping(value = "/script_form_save.htm")
	public String scriptFormSave(ScriptDO scriptDO,String begin_time,String end_time) {
		scriptDO.setBeginTime(DateUtil.parse(begin_time));
		scriptDO.setEndTime(DateUtil.parse(end_time));
		JSONObject json = new JSONObject();
		boolean exist = scriptService.get(ScriptDO.class, scriptDO.getId()) != null;
		json.put("code", exist ? scriptService.updateSkipCheckOwner(scriptDO) : scriptService.create(scriptDO));
		json.put("id", scriptDO.getId());
		return json.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/script_start_listen.htm")
	public String scriptStartListen(String id) {
		JSONObject json = new JSONObject();
		json.put("code", scriptService.listen(id));
		json.put("id", id);
		return json.toJSONString();
	}

	@ResponseBody
	@RequestMapping(value = "/script_stop_listen.htm")
	public String scriptStopListen(String id) {
		JSONObject json = new JSONObject();
		json.put("code", scriptService.stopListen(id));
		json.put("id", id);
		return json.toJSONString();
	}

}
